package com.example.demo01.demo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo01.R;
import com.example.demo01.consts.ResultData;
import com.example.demo01.entity.UserDemo;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.Utils;

public class IntentDemo extends AppCompatActivity {
    Utils utils = new Utils(this);
    //. 方式二，接收返回结果与跳转Activity 实例
    private ActivityResultLauncher<Intent> activityResultLauncher;
    private Button btn_to, btn_to_results, btn_to_url;
    EditText et_request_name, et_request_nickname, ev_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance(getResources().getString(R.string.IntentDemo)))
                .commit();

        initView();
    }

    private void initView() {
        btn_to = findViewById(R.id.btn_to);
        btn_to_results = findViewById(R.id.btn_to_results);
        et_request_name = findViewById(R.id.et_request_name);
        et_request_nickname = findViewById(R.id.et_request_nickname);
        ev_url = findViewById(R.id.ev_url);
        btn_to_url = findViewById(R.id.btn_to_url);


        addListener();
    }

    private void addListener() {
        //. 注册方式二接收返回结果的实例
        activityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    //. 在这里处理返回结果
                    if (result.getResultCode() == RESULT_OK) {
                        Intent data = result.getData();
                        if (data != null) {
                            btn_to_results.setText(data.getStringExtra(ResultData.RESULT_STRING_KEY));
                        }
                    }
                }
        );

        //. 带参跳转
        btn_to.setOnClickListener(v -> {
            Intent intent = new Intent(IntentDemo.this, IntentTestPage.class);
            UserDemo user = new UserDemo(1, et_request_name.getText().toString(), et_request_nickname.getText().toString());
            //! 这里需要将 UserDemo 类实现 Serializable 接口才可以传递对象，可以点到 UserDemo 实体类里面看看
            //！ 说白了实现这个接口是为了将对象序列化成便于传输的格式
            intent.putExtra("user", user);
            startActivity(intent);
        });

        //. 带参跳转并获取返回结果
        btn_to_results.setOnClickListener(v -> {
            Intent intent = new Intent(IntentDemo.this, IntentTestPage.class);

            //! 这里注意 requestCode 不能设置为负数只能设置成正整数，他只是用来规定的请求标识
            //! 怕一个Activity里面有两个需要跳转到其他页面获取数据时做区分
            //. 方式一(已废弃)
//                startActivityForResult(intent, 1);
            //. 方式二(推荐)
            activityResultLauncher.launch(intent);
        });

        //! 跳转网页需要在 AndroidManifest.xml 中申请网络权限 <uses-permission android:name="android.permission.INTERNET" />
        btn_to_url.setOnClickListener(v -> {
            String uri = ev_url.getText().toString();
            // 正则表达式验证是否包含(http|https)是网址
            if (!uri.matches("^(http|https)://.*$")) {
                uri = "http://" + uri;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            startActivity(intent);
        });
    }


    //. 方式一：接收返回结果回调
    //？ 这个函数用来接收第二个Activity返回的结果
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//
//        if (resultCode == RESULT_OK && data != null && requestCode == 1) {
//            btn_to_results.setText(data.getStringExtra(ResultData.RESULT_STRING_KEY));
//        }
//    }
}